/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2020-09-01
 * Time: 7:43
 */
#include <iostream>
#include <thread>
#include "Header.h"

using namespace std;

class thread_guard
{
    std::thread &t;

public:
    explicit thread_guard(std::thread &_t)
        : t(_t)
    {
    
    }
    
    virtual ~thread_guard()
    {
        if (t.joinable())       //1
            t.join();           //2
    }
    
    thread_guard(thread_guard const &) = delete;        //3
    
    thread_guard &operator=(thread_guard const &) = delete;
};

void do_something_in_current_thread()
{
    cout << "hello world" << endl;
}

typedef int widget_id;

typedef int widget_data;

void update_data_for_widget(widget_id w, widget_data & data){};// 1

void display_status()
{

}

void process_widget_data(widget_data basicString)
{

}

void oops_again(widget_id w)
{
    widget_data data;
    //std::thread t(update_data_for_widget, w, data); // 2
    std::thread t(update_data_for_widget, w, std::ref(data));       //2
    display_status();
    t.join();
    process_widget_data(data);
}

int main()
{
    int some_local_state = 0;
    func my_func(some_local_state);
    std::thread t(my_func);
    thread_guard guard(t);
    do_something_in_current_thread();
    return 0;
}   //4
